package cn.itsource.ldj.org.controller;

import cn.itsource.ldj.org.domain.Detail;
import cn.itsource.ldj.org.domain.Dictionaries;
import cn.itsource.ldj.org.query.DictionariesQuery;
import cn.itsource.ldj.org.service.IDictionariesService;
import cn.itsource.ldj.util.AjaxResult;
import cn.itsource.ldj.util.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Dictionaries")
public class DictionariesController {

    @Autowired
    private IDictionariesService dictionariesService;


    @GetMapping
    public List<Dictionaries> findAll() {
        return dictionariesService.findAll();
    }


    @GetMapping("/{id}")
    public Dictionaries findOne(@PathVariable("id") Long id){
        return dictionariesService.findOne(id);
    }


    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Dictionaries dictionaries){
        try {
            if (dictionaries.getId() != null) {
                dictionariesService.update(dictionaries);
            } else {
                dictionariesService.save(dictionaries);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }


    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionariesService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }


    @PostMapping
    public QueryResult<Dictionaries> queryPage(@RequestBody DictionariesQuery query) {
        return dictionariesService.queryPage(query);
    }


    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        ids.forEach(System.out::println);
        try {
            dictionariesService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    @PostMapping("/detail")
    public QueryResult<Detail> findAllDetail(@RequestBody DictionariesQuery query){
        return dictionariesService.findAllDetail(query);
    }


    @PutMapping("/detail")
    public AjaxResult saveOrUpdate2(@RequestBody Detail detail){
        try {
            if (detail.getId() != null) {
                dictionariesService.detailUpdate(detail);
            } else {
                dictionariesService.detailSave(detail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }


    @DeleteMapping("detail/{id}")
    public AjaxResult delete2(@PathVariable("id") Long id) {
        try {
            dictionariesService.detailDelete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }
}
